#include "graph.hh"
#include "painter.hh"

namespace icesp
{
    void renumber(graph::adj_matrix& g, painter& p)
    {
        auto& n = g.n;
        auto& m = g.m;
        auto&  index = g. index;
        auto& rindex = g.rindex;
        auto& node_number_count = p.node_number_count;

        std::vector<int> tmp(p.nthread);

        tmp[0] = 0;
        for (int i = 1; i < p.nthread; i++)
            tmp[i] = tmp[i - 1] + node_number_count[i - 1];
        for (int i = 0; i < n; i++) {
            auto& co = p.colors[i];
            index[i] = tmp[co];
            rindex[tmp[co]] = i;
            tmp[co]++;
        }

        std::vector<graph::edge> edges(m);
        std::vector<int> edge_index(n + 1);

        for (auto i = 0, j = 0; i < n; i++) {
            auto& v = rindex[i];
            for (int k = g.edge_index[v]; k < g.edge_index[v + 1]; j++, k++) {
                edges[j].from   = rindex[g.edges[k].from];
                edges[j].to     = rindex[g.edges[k].to];
                edges[j].weight = g.edges[k].weight;
            }
        }

        std::swap(edges, g.edges);
        std::swap(edge_index, g.edge_index);

        g.build_index();

        // TODO build color label
    }
}

